<?php
global $_W, $_GPC;

$act = $_GPC['act'];

if ($act == 'list') {
    $page = $_GPC['page'] ? $_GPC['page'] : 1;
    $pageSize = max($_GPC['pageSize'], 20);
    $categoryId = $_GPC['categoryId'];
    $condition = array();
    if ($categoryId) {
        $condition['category_id'] = $categoryId;
    }
    $keyField = '';
    $fields = array('id', 'title', 'writer', 'thumb');
    $orderBy = array('sort desc', 'create_time desc');
    $limit = array($page, $pageSize);
    $list = M('article')->getAll($condition, $fields, $keyField, $orderBy, $limit);
    if (!empty($list)) {
        foreach ($list as $key => $value) {
            $list[$key]['thumb'] = tomedia($value['thumb']);
        }
    }
    $this->result(0, '获取到文章列表', $list);
}

if ($act == 'detail') {
    $id = intval($_GPC['id']);
    $detail = M('article')->getInfo($id);
    // 更新阅读次数
    if ($id) {
        $data = array(
            'read_count +=' => 1,
            'id' => $id
        );
        M('article')->update($data);
    }

    $detail['thumb'] = tomedia($detail['thumb']);
    $detail['content'] = htmlspecialchars_decode($detail['content']);
    $this->result(0, '获取到文章内容', $detail);
}
